package com.ad.pet.common.data.utils.upload;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.ad.pet.Logger;
import com.ad.pet.common.data.http.HttpCode;
import com.ad.pet.common.data.http.HttpHelp;
import com.ad.pet.common.data.utils.FileUtil;
import com.ad.pet.common.data.utils.JsonUtil;
import com.ad.pet.models.result.ResultBean;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by zwb on.
 * Des:
 * Date: 2017/8/11
 */

public class UploadService extends IntentService {
    private static final String TAG = "UploadService";
    OkHttpClient mOkHttpClient;
    @Inject
    HttpHelp mHttpHelp;
    @Inject
    JsonUtil mJsonUtil;

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     */
    public UploadService() {
        super("UploadService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d(TAG, "onHandleIntent");
        if (intent != null) {
            String url = intent.getStringExtra("url");
            String phone = intent.getStringExtra("phone");
            try {
                uploadFile(phone, url);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void onCreate() {
        super.onCreate();
//        DaggerServiceComponent.builder().applicationComponent(App.getApp().getApplicationComponent())
//                .serviceModule(new ServiceModule(this))
//                .build().inject(this);
        mOkHttpClient = mHttpHelp.getOkHttpClient();

    }


    private void uploadFile(String phone, String url) throws IOException {
        String zip = Logger.zip();
        if (TextUtils.isEmpty(zip)) {
            throw new NullPointerException("zip file not exists");
        }
        Log.d(TAG, "uploadFile");
        FileInputStream inputStream = new FileInputStream(new File(zip));
        int len = inputStream.available();
        byte[] buffer = new byte[len];
        inputStream.read(buffer);
        inputStream.close();
        RequestBody file_body = RequestBody.create(MediaType.parse("application/octet-stream"), buffer);

        RequestBody request_body = new MultipartBody.Builder().
                setType(MultipartBody.FORM)
                .addFormDataPart("phone", phone)
                .addFormDataPart("content", "崩溃日志")
                .addFormDataPart("zip", "upfile.zip", file_body)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "multipart/form-data")
                .post(request_body)
                .build();
        Response response = mOkHttpClient.newCall(request).execute();
        //是否成功
        if (response != null && response.isSuccessful()) {
            ResultBean resultBean = mJsonUtil.fromJson(response.body().string(), ResultBean.class);
            if (HttpCode.SUCCESS.equals(resultBean.getCode())) {
                Log.d(TAG, "success");
                FileUtil.getInstance().deleteFolder(FileUtil.getInstance().getLogDir().getPath());
            }
        } else {
            Log.d(TAG, "onFailure");
        }
    }
}
